/* 
 * Amnesia is Copyright (c) 2010 Mark Russell
 * 
 * Contact: info@amnesia-app.com	
 * 
 * This file is part of Amnesia.
 * 
 * Amnesia is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Amnesia is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Amnesia. If not, see <http://www.gnu.org/licenses/>.
 *
 */

// -------------------------------------------------------------
// Amnesia Register new key Code
// -------------------------------------------------------------

var user = "";
var pass = "";
var pass_2 = "";
var email_regex = "";

var username = "";
var firstname = "";
var lastname = "";
var master_key_1 = "";
var master_key_2 = "";
var error = "";
var error_flag;

$(document).ready(function() {
	// -------------------------------------------------------------
	// Basic registration functions
	// -------------------------------------------------------------
	
	function set_page_basic_reg(){
		$("#error_email_valid").hide();
		$("#error_pass_blank").hide();
		$("#error_pass_match").hide();
		$("#error_pass_length").hide();
		check_email_address();
	}
	
	function check_email_address(){
		
		user = $("input#user").attr('value');
		email_regex = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/;
		
		if(user == "" | !user.match(email_regex)){
			$("#error_email_valid").show();
			return false;
		}
		check_password();
	}
	
	function check_password(){
		
		pass = $("input#pass").attr('value');
		pass_2 = $("input#pass_2").attr('value');
		
		if(pass == "" | pass_2 == ""){
			$("#error_pass_blank").show();
			return false;
		} else if (pass != pass_2){
			$("#error_pass_match").show();
			return false;
		}
		password_length();
	}
	
	function password_length(){
		
		pass_length = $("input#pass").attr('value');
		
		if(pass_length.length <= 7){
			$("#error_pass_length").show();
			return false;
		}
		
		submit_it();
	}
	
	// Submit form
	
	function submit_it(){
		document.reg.submit();
	}
	
	$("#register").click(function() {
		set_page_basic_reg()
	});
	
	

	// -------------------------------------------------------------
	// Master Key functions - Check fields, prep key and submit
	// -------------------------------------------------------------
	
	function set_page(){
		username = $("input#username").attr('value');
		$("#key_entry_error").hide();
		$("#error_firstname_blank").hide();
		$("#error_firstname").hide();
		$("#error_lastname_blank").hide();
		$("#error_lastname").hide();
		$("#error_master_key").hide();
		$("#error_master_key_length").hide();
		error_flag = 0;
		check_first_name();
	}
	
	function check_first_name(){
	
		firstname = $("input#firstname").attr('value');
		
		if(firstname ==""){
			$("#key_entry_error").show();
			$("#error_firstname_blank").show();
			error_flag = 1;
		} else if(firstname.length > 30){
			$("#key_entry_error").show();
			$("#error_firstname").show();
			error_flag = 1;
		}
		check_last_name();
	}
	
	function check_last_name(){
	
		lastname = $("input#lastname").attr('value');

		if(lastname ==""){
			$("#key_entry_error").show();
			$("#error_lastname_blank").show();
			error_flag = 1;
		} else if(lastname.length > 30){
			$("#key_entry_error").show();
			$("#error_lastname").show();
			error_flag = 1;
		}
		check_key();
	}
	
	function check_key(){
		
		master_key_1 = $("input#master_key_1").attr('value');
		master_key_2 = $("input#master_key_2").attr('value');
		
		if (master_key_1 =="" | master_key_1 != master_key_2) {
			$("#key_entry_error").show();
			$("#error_master_key").show();
			error_flag = 1;
			return false;
		}
		check_key_length();
	}
	
	function check_key_length(){
		
		key_length = $("input#master_key_1").attr('value');
		
		if(key_length.length <= 11) {
			$("#key_entry_error").show();
			$("#error_master_key_length").show();
			error_flag = 1;
			return false;
		} else {
			proceed();
		}
	}
	
	function proceed() {
	
		if(!error_flag){
	
			// Encode the check string.
			var check = "checker";
			
			var hashed = Passpack.encode("SHA256",master_key_1);
			var check_string = Passpack.encode("AES",check,hashed);
			
			// Send vars
			var query = "firstname=" + firstname + "&lastname=" + lastname + "&check_string=" + check_string + "&username=" + username;
		
			$.ajax({
			type: "POST",
			url: "update_user.php",
			data: query,
			success: function() {
				window.location.replace("registration_complete.php?firstname=" + firstname + "&lastname=" + lastname);
			}
			});
		
		}
	}
	
	$("#create_key").click(function() {
		set_page()
	});
	
	// -------------------------------------------------------------
	// Password Generator
	// -------------------------------------------------------------
	
	var password = "";
	
	$(".pwd_gen").click(function () {
		password = $.password(12);
		$("#suggested").replaceWith("<span id=\"suggested\">" + password + "</span>");
		$("#suggest").show();
		$(".pwd_use").show();
		$("#reg_password_suggest").css("background-color","#e1e2e3");
		$("#reg_password_suggest").css("border","1px solid #b0b3b7");
	});
	
	$(".pwd_use").click(function () {
		$("input#pass").attr("value", password);
		$("input#pass_2").attr("value", password);
	});
	
	// -------------------------------------------------------------
	// Show/hide input text in master key fields
	// -------------------------------------------------------------
	
	$('#master_key_1').showPassword('#show_pwd');
	$('#master_key_2').showPassword('#show_pwd');
});